package com.cloud.netty.tcp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @program: netty
 * @description:
 * @author: Cloud
 * @create: 2022/4/14 14:20:11
 */
public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private int index;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("连接上了Server");
        for (int i = 0; i < 200; i++) {
            ByteBuf buffer = Unpooled.copiedBuffer("hello server" + i + " ", CharsetUtil.UTF_8);
            ctx.writeAndFlush(buffer);
            System.out.println(++index);
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
